package org.crmsystem.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.dao.ProKindMapper;
import org.crmsystem.model.File;
import org.crmsystem.model.OrgInfo;
import org.crmsystem.model.Pact;
import org.crmsystem.model.ProKind;
import org.crmsystem.model.Staff;
import org.crmsystem.service.IFileService;
import org.crmsystem.service.IOrgService;
import org.crmsystem.service.IProductService;
import org.crmsystem.service.IStaffService;
import org.crmsystem.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 资料（File）控制层
 * 
 * @author 袁立新
 *
 */

@Controller
@RequestMapping("/file")
public class FileController {
	private static Logger logger = LogManager.getLogger(ProKindMapper.class);
	/**
	 * 资料Service接口
	 */
	@Autowired
	private IFileService iFileService;
	@Autowired
	private IProductService iProductService;
	@Autowired
	private IOrgService iOrgService;
	@Autowired
	private IStaffService iStaffService;
	
	/**
	 * 时间对象
	 */
	DateUtil dateUtil = new DateUtil();

	/**
	 * 添加资料页面
	 * 
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/add")
	public String getIndex(Map map) throws ParseException {		
		ArrayList<OrgInfo> orgList=iOrgService.selectOrgList(null);//机构		
		Map<String,Object> map1=new HashMap<String, Object>();//建集合
		map.put("orgList", orgList);//把orgList放到map中		
		return "addFile";
	}

	/**
	 * 资料列表页面
	 * 
	 * @return
	 */
	@RequestMapping("/showFile")
	public ModelAndView getIndex1(File file) {
		ArrayList<File> files = (ArrayList<File>)iFileService.findFiles(file);
		ArrayList<OrgInfo> orgList=iOrgService.selectOrgList(null);//机构		
		Map<String,Object> map=new HashMap<String, Object>();//建集合
		map.put("orgList", orgList);//把orgList放到map中
		ModelAndView moView = new ModelAndView("fileList");//显示fileList
		moView.addObject("orgLt",map);
		moView.addObject("files", files);
		return moView;		
	}

	/**
	 * 添加资料action
	 * 
	 * @param file
	 * @param trade
	 * @param dateStr
	 * @param contactIdStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveFile",method=RequestMethod.POST)
	public String saveFile(int id,int orgId,String name,String kind,int operatorId,
			String createDate,String type,String remark) throws Exception {
        File file=new File();
        file.setId(id);//编号
        file.setOrgId(orgId);//机构编号  名称
        file.setName(name);//资料名称      
        file.setKind(kind);//分类
        file.setOperatorId(operatorId);//发布者    
        Date date1;
        if(!"".equals(createDate)){
        	date1=DateUtil.formatDate(createDate, "yyyy-MM-dd");       			 	
        }else{
        	date1=new Date();//发布日期
        }     
        file.setCreateDate(date1);        
        file.setType(type);//类型
        file.setRemark(remark);//备注
        int flag=iFileService.saveFile(file);
        
		return "redirect:/file/showFile";
	}
	
	/**删除资料
	 * @param fileId
	 * @return
	 */
	@RequestMapping("/deleFile")
	@ResponseBody
    public String delFile(int id){
    	int flag=iFileService.delete(id);
    	String msg = null;
        logger.info(flag + "======");
        if (flag == 1) {
            msg = "SUCC";
        } else {
            msg = "";
        }
        return msg;   	
    }

	@RequestMapping("/updateFile")
	public ModelAndView updateFile(int fileId) throws ParseException{
		File file=iFileService.findFileById(fileId);
		ModelAndView mView=new ModelAndView("addFile");
		Map<String,Object> map=new HashMap<String, Object>();
		List<OrgInfo> orgLists=iOrgService.selectOrgList(null);//机构
		map.put("orgList", orgLists);
		mView.addObject("opList",map);
		mView.addObject("file",file);
		return mView;
	}
	/*@RequestMapping("/updatePact")
	public ModelAndView updatePact(String  pactId) throws ParseException {
		Pact pact=iPactService.findPactById(pactId);
		ModelAndView mView=new ModelAndView("addPact");
		Map<String,Object> map=new HashMap<String, Object>();
		List<OrgInfo> orgLists=iOrgService.selectOrgList(null);//机构
		List<ProKind> proKinds = iProKindService.select();//产品类别
		map.put("orgList", orgLists);
		map.put("proKinds", proKinds);				    	    	
		mView.addObject("opList",map);
		mView.addObject("pact",pact);								
		return mView;
			
	}*/
    
}
